package com.community.zhihuishequ.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.community.zhihuishequ.R;
//import com.scwang.smartrefresh.layout.SmartRefreshLayout;

/**
 * Created by Administrator on 2018/2/7.
 */

public class LoadingLayout extends FrameLayout {
    /**
     * 空数据的view
     */
    private int mEmptyView;
    /**
     * 状态view
     */
    private int mStateView;
    /**
     * 加载view
     */
    private int mLoadingView;
    /**
     * 列表view
     */
    private int mRecyclerView;

    public LoadingLayout(Context context) {
        this(context, null);
    }

    public LoadingLayout(Context context, AttributeSet attrs) {
        this(context, attrs, -1);
    }

    public LoadingLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.LoadingLayout, 0, 0);
        try {
            mStateView = a.getResourceId(R.styleable.LoadingLayout_stateView, R.layout.load_state_layout);
            mLoadingView = a.getResourceId(R.styleable.LoadingLayout_loadingView, R.layout.loading_layout);
            mEmptyView = a.getResourceId(R.styleable.LoadingLayout_emptyView, R.layout.empty_laoyout);
            mRecyclerView = a.getResourceId(R.styleable.LoadingLayout_recyclerview, R.layout.loading_list);
            LayoutInflater inflater = LayoutInflater.from(getContext());
            inflater.inflate(mStateView, this, true);
            inflater.inflate(mLoadingView, this, true);
            inflater.inflate(mEmptyView, this, true);
            inflater.inflate(mRecyclerView, this, true);
        } finally {
            a.recycle();
        }
    }

    /**
     * 布局加载完成后隐藏所有view
     */
    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        for (int i = 0; i < getChildCount(); i++) {
            getChildAt(i).setVisibility(GONE);
        }
    }

    /**
     * 获取RecyclerView
     */
    public RecyclerView getRecyclerView() {
        for (int i = 0; i < this.getChildCount(); i++) {
            View child = this.getChildAt(i);
            if (i == 3) {
                return ((RecyclerView) findViewById(R.id.load_list));
            }
        }
        return null;
    }

    /**
     * 获取SmartRefreshLayout
     */
//    public SmartRefreshLayout getSmartRefreshLayout() {
//        for (int i = 0; i < this.getChildCount(); i++) {
//            View child = this.getChildAt(i);
//            if (i == 3) {
//                return ((SmartRefreshLayout) findViewById(R.id.load_ref));
//            }
//        }
//        return null;
//    }

    /**
     * 这是empty点击事件
     */
    public void setEmptyClickListener(final OnClickListener listener) {
        if (listener != null) {
            findViewById(R.id.btn_empty).setOnClickListener(listener);
        }
    }

    /**
     * 设置state点击事件
     */
    public void setStateClickListener(OnClickListener listener) {
        if (listener != null) {
            findViewById(R.id.btn_load_state).setOnClickListener(listener);
        }
    }

    /**
     * 设置自定义布局的点击事件
     */
    public void setViewOnClickListener(int resourId, OnClickListener listener) {
        findViewById(resourId).setOnClickListener(listener);
    }

    /**
     * 设置自定义布局的view文本
     */
    public void setViewText(int resoureId, String text) {
        ((TextView) findViewById(resoureId)).setText(text);
    }

    /**
     * 设置自定义布局的image
     */
    public void setViewImage(int resoureId, int img) {
        ((ImageView) findViewById(resoureId)).setImageResource(img);
    }

    /**
     * state view
     */
    public void showState() {
        for (int i = 0; i < this.getChildCount(); i++) {
            View child = this.getChildAt(i);
            if (i == 0) {
                child.setVisibility(VISIBLE);
            } else {
                child.setVisibility(GONE);
            }
        }
    }

    /**
     * empty view
     */
    public void showEmpty() {
        for (int i = 0; i < this.getChildCount(); i++) {
            View child = this.getChildAt(i);
            if (i == 2) {
                child.setVisibility(VISIBLE);
            } else {
                child.setVisibility(GONE);
            }
        }
    }

    /**
     * Loading view
     */
    public void showLoading() {
        for (int i = 0; i < this.getChildCount(); i++) {
            View child = this.getChildAt(i);
            if (i == 1) {
                child.setVisibility(VISIBLE);
            } else {
                child.setVisibility(GONE);
            }
        }
    }

    public void showLoading(String text) {
        for (int i = 0; i < this.getChildCount(); i++) {
            View child = this.getChildAt(i);
            if (i == 1) {
                child.setVisibility(VISIBLE);
                ((TextView) child.findViewById(R.id.loading_text)).setText(text + "");
            } else {
                child.setVisibility(GONE);
            }
        }
    }

    public void showEmpty(String text) {
        for (int i = 0; i < this.getChildCount(); i++) {
            View child = this.getChildAt(i);
            if (i == 2) {
                child.setVisibility(VISIBLE);
                ((TextView) child.findViewById(R.id.empty_text)).setText(text + "");
            } else {
                child.setVisibility(GONE);
            }
        }
    }

    public void showState(String tips) {
        for (int i = 0; i < this.getChildCount(); i++) {
            View child = this.getChildAt(i);
            if (i == 0) {
                child.setVisibility(VISIBLE);
                ((TextView) child.findViewById(R.id.load_state_tv)).setText(tips + "");
            } else {
                child.setVisibility(GONE);
            }
        }
    }

    public void showState(int stateId, String tips) {
        for (int i = 0; i < this.getChildCount(); i++) {
            View child = this.getChildAt(i);
            if (i == 0) {
                child.setVisibility(VISIBLE);
                ((ImageView) child.findViewById(R.id.load_state_img)).setImageResource(stateId);
                ((TextView) child.findViewById(R.id.load_state_tv)).setText(tips + "");
            } else {
                child.setVisibility(GONE);
            }
        }
    }

    /**
     * 展示内容
     */
    public void showContent() {
        for (int i = 0; i < this.getChildCount(); i++) {
            View child = this.getChildAt(i);
            if (i == 3) {
                child.setVisibility(VISIBLE);
            } else {
                child.setVisibility(GONE);
            }
        }
    }
}
